package com.derbysoft.ota.application.util;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class Dates {

	public static DateFormat defaultDateFormat() {
		return new SimpleDateFormat("yyyy-MM-dd");
	}

	public static Date parse(String date) {
		return parse(date, defaultDateFormat());
	}

	public static Date parse(String date, DateFormat df) {
		if (StringUtils.isBlank(date)) {
			return null;
		}
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new IllegalArgumentException("Couldn't parse date [" + date + "] with format [" + defaultDateFormat() + "]", e);
		}
	}

	public static String format(Date date) {
		return format(date, defaultDateFormat());
	}

	public static String format(Date date, DateFormat df) {
		return df.format(date);
	}


}
